﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GauntletFramework.SpriteManagment
{
    public class SpriteSheetAnimation
    {
        public SpriteSheetAnimation(int StartFrame, int EndFrame, float FrameTime, bool Loop, SpriteSheet SpriteSheet)
        {
            this.StartFrame = StartFrame;
            this.EndFrame = EndFrame;
            this.FrameTime = FrameTime;
            this.Loop = Loop;
            this.SpriteSheet = SpriteSheet;

            CurrentFrame = StartFrame;
            CurrentFrameTime = FrameTime;
        }
        public SpriteSheetAnimation(SpriteSheetAnimation SSA)
        {
            this.StartFrame = SSA.StartFrame;
            this.EndFrame = SSA.EndFrame;
            this.FrameTime = SSA.FrameTime;
            this.Loop = SSA.Loop;
            this.SpriteSheet = SSA.SpriteSheet;

            CurrentFrame = SSA.StartFrame;
            CurrentFrameTime = SSA.FrameTime;
        }

        int StartFrame;
        int EndFrame;
        float FrameTime;
        bool Loop;
        public SpriteSheet SpriteSheet;

        public bool Finished;

        int CurrentFrame;
        float CurrentFrameTime;

        public void Update(float Time)
        {
            CurrentFrameTime -= Time;
            if (CurrentFrameTime <= 0)
            {
                CurrentFrameTime = FrameTime;
                CurrentFrame++;
            }
            if (CurrentFrame > EndFrame)
            {
                CurrentFrame = StartFrame;
                if (!Loop)
                    Finished = true;
            }
        }

        public void Draw(SpriteBatch SpriteBatch, Vector2 Position, Color Color)
        {
            if (SpriteSheet != null)
                SpriteBatch.Draw(SpriteSheet.Sprites, Position, SpriteSheet.GetRectangle(CurrentFrame), Color);
            else
                Debug.Fail("No SpriteSheet");
        }
        public void Draw(SpriteBatch SpriteBatch, Rectangle Destination, Color Color)
        {
            if (SpriteSheet != null)
                SpriteBatch.Draw(SpriteSheet.Sprites, Destination, SpriteSheet.GetRectangle(CurrentFrame), Color);
            else
                Debug.Fail("No SpriteSheet");
        }
        public void Draw(SpriteBatch SpriteBatch, Rectangle destination, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth)
        {
            if (SpriteSheet != null)
                SpriteBatch.Draw(SpriteSheet.Sprites, destination, SpriteSheet.GetRectangle(CurrentFrame), color, rotation, origin, effects, layerDepth);
            else
                Debug.Fail("No SpriteSheet");
        }
        public void Draw(SpriteBatch SpriteBatch, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
        {
            if (SpriteSheet != null)
                SpriteBatch.Draw(SpriteSheet.Sprites, position, SpriteSheet.GetRectangle(CurrentFrame), color, rotation, origin, scale, effects, layerDepth);
            else
                Debug.Fail("No SpriteSheet");
        }
        public void Draw(SpriteBatch SpriteBatch, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
        {
            if (SpriteSheet != null)
                SpriteBatch.Draw(SpriteSheet.Sprites, position, SpriteSheet.GetRectangle(CurrentFrame), color, rotation, origin, scale, effects, layerDepth);
            else
                Debug.Fail("No SpriteSheet");
        }
    }
}
